<%@ page language="java" pageEncoding="UTF-8"%>
<style>
.l-group>span {
  margin-left: 30px;
  vertical-align: middle;
  display: inline-block;
    width: auto;
}
.l-form .l-group{
	padding-top: 0px;
}
.l-group {
    width: auto;
    padding: 0px;
    border-left: 0px;
    background: none;
}
</style>
<!-- 导入页面 -->
<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="editCheckLabel" aria-hidden="true">
	<div class="modal-dialog" style="width: 800px;">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<p class="modal-title" id="editCheckLabel">${module.moduleName}编辑</p>
			</div>
			<div class="modal-body cont-table">
				<div class="table-normal">
					<div class="l-form">
						<form id="importForm" action="${baseURL }/jdbc/common/basecommonimport/baseImportForResult.do?mdId=${param.mdId}&mdCode=${param.mdCode}" method="post" enctype="multipart/form-data">
							<div class="l-group l-group-hasicon" style="margin: 5px 0px;">
								<i class="fa fa-upload fa-title"></i> 导入文件：
							</div>
							<div class="input-group width-100">
								<input id="show-attach-name" name="attachName" type="text" class="form-control border" readonly="readonly" />
								<div class="input-group-addon upload-file">
									<input onchange="chooseFile(this, '#show-attach-name');" type="file" class="file-select" id="attach" name="attach">
									<button class="btn btn-small btn-blue">上传</button>
								</div>
							</div>
							<div style="padding: 15px 0px 5px 0px;font-size: 12px;color: red;font-style: normal;font-weight: 600;">
								导入模板允许删除不需要的列或调整列顺序
								<a href="#" style="float: right;" onclick="loadBaseTemplate();return false;">模板下载</a>
							</div>
						</form>
					</div>
					<div class="l-form" style="margin-bottom: 0px;height: auto; min-height: 400px;border-top: 1px solid #ccc;overflow: auto;">
						<div class="l-group l-group-hasicon" style="margin:5px 0px;">
							<i class="fa fa-upload fa-title"></i> 导入结果：
						</div>
						<div id="upload_error" style="font: 12px;color:#505050 ; padding-left: 10px;"></div>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-blue" id="importBtn"> 导入 </button>
				<button type="button" class="btn btn-gray" data-dismiss="modal" id="closeBtn"> 关闭 </button>
			</div>
		</div>
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript">

	$("#importBtn").click(function(){
		try{
			if (ev_validate()){
				ev_import();
			}
		}catch(e){
			console.info(e);
		}
	});
	
	$("#closeBtn").click(function(){
		$("#upload_error").empty();
		document.getElementById("importForm").reset();
	});
	
	$('#importModal').on('hide.bs.modal', function () {
		$("#upload_error").empty();
		document.getElementById("importForm").reset();
	})
	
    function ev_import(){
    	//处理是否写了传参函数getFieldValue
    	try {
	    	if(getFieldValue && typeof(getFieldValue)=="function"){//在需要导入的页面写，用于传参，值类型为{entityField:value,entityField:value}
	    		var fieldValue = getFieldValue();
	    		var url = fieldValue['url'];
	    		if(url == undefined) url = "";
	    	}
	    	if(url == ''){//没指定url
	    		var mdId = fieldValue['mdId'];
	    		var mdCode = fieldValue['mdCode'];//可以自定义mdCode，为空则用默认的
	    		if(mdId == undefined) mdId = "";
	    		if(mdCode == undefined) mdCode = "";
		    	if(mdCode != ''){//指定了mdCode
					var url = "${baseURL }/jdbc/common/basecommonimport/baseImportForResult.do?mdId="+mdId+"&mdCode="+mdCode+"&fieldValue="+JSON.stringify(fieldValue);
		    	}else{//默认url + 参数
			    	var url = "${baseURL }/jdbc/common/basecommonimport/baseImportForResult.do?mdId=${param.mdId}&mdCode=${param.mdCode}&fieldValue="+JSON.stringify(fieldValue);
		    	}
	    	}
    	}catch(e){//默认url
    		var url = "${baseURL }/jdbc/common/basecommonimport/baseImportForResult.do?mdId=${param.mdId}&mdCode=${param.mdCode}";
    	}
        $.ajaxFileUpload({
            url:url,
            secureuri:false,
            fileElementId:"attach",
            dataType:"text",
            success:function(data){
            	data = data.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
                $("#upload_error").empty();//清除掉之前的错误提示，若需要显示则此行代码删除
                $("#upload_error").append(data);
                if(typeof(importCallback)=="function"){
                	importCallback();
                }
                if(typeof(refreshTable)=="function"){
                	refreshTable();
                }
                $.search();
            }, 
            //服务器响应失败
            error:function(data,status,e){
            	console.info(e);
            }
        });
    }
    
    //检查上传文件的格式是否正确
    function checkFile(path){
        var errorType = "";
        var suffix =path.match(/^(.*)(\.)(.{1,8})$/)[3].toLowerCase();
        if(suffix != "xls"){
            errorType = "抱歉，目前仅支持xls格式的文件!";
        }else{
            errorType = "";
        }
        return errorType;
    }
	    
    function ev_validate() {
    	var path = $("#attach").val();
    	if (path == "") {
    		//$.ligerDialog.error("请选择导入文件!");
    		BT.showError("请选择导入文件!");
    		return false;
    	}
    	var errormsg = checkFile(path);
    	if(errormsg != ""){
    		BT.showError(errormsg);
    		//$("#upload_error").append(errormsg+"<br>");
            //$.ligerDialog.error(errormsg);
            return false;
        }
    	return true;
    }
    
    function loadBaseTemplate() {
    	//处理是否写了传参函数getFieldValue
    	try {
    		var mdCode = '';
	    	if(getFieldValue && typeof(getFieldValue)=="function"){//在需要导入的页面写，用于传参，值类型为{entityField:value,entityField:value}
	    		var fieldValue = getFieldValue();
	    		var mdId = fieldValue['mdId'];
	    		mdCode = fieldValue['mdCode'];//可以自定义mdCode，为空则用默认的
	    		if(mdId == undefined) mdId = "";
	    		if(mdCode == undefined) mdCode = "";
	    	}
	    	if(mdCode != ''){
				var url = "${baseURL }/jdbc/common/basecommonimport/templateDown.do?mdId="+mdId+"&mdCode="+mdCode;
	    	}else{
		    	var url = "${baseURL }/jdbc/common/basecommonimport/templateDown.do?mdId=${param.mdId}&mdCode=${param.mdCode}";
	    	}
    	}catch(e){
    		var url = "${baseURL }/jdbc/common/basecommonimport/templateDown.do?mdId=${param.mdId}&mdCode=${param.mdCode}";
    	} 
		window.location.href = url;
	}
    
    function chooseFile(obj, selector) {
		$(selector).val(obj.files[0].name);
	}
</script>
